package com.hue.concurrent.day04;

import java.util.concurrent.locks.ReentrantLock;

/*
    ReentrantLock设置可中断锁
 */
public class Demo08 {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Thread t1 = new Thread(() -> {
            try {
                System.out.println("t1尝试得到lock");
                //如果没有竞争该方法会得到锁
                //如果有竞争该线程就会进入阻塞队列，其他线程调用该线程的interrupt方法，就会中断
                lock.lockInterruptibly();
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("t1没有获取到锁，返回");
                return;
            } finally {
                lock.unlock();
            }
        });
        lock.lock();
        t1.start();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
    }
}
